#include "TABDMP1Data.h"

#include "Utility/Logging/SGLogger.h"
#include "DataStructure/Common/Validation.h"

using namespace SG::DataStructure::FEM;
SG::Algebra::Real_t TABDMP1Data::Interpolate (const SG::Algebra::Real_t& omega) const
{
    // 获取已有数据点
    const auto& points = m_DAMPList;

    // 获取数据范围
    const auto& x_min = points.front ().m_F;
    const auto& x_max = points.back ().m_F;

    // 如果omega小于最小x值，使用前两个点进行外推
    if (omega < x_min)
    {
        const auto& x1 = points[0].m_F;
        const auto& x2 = points[1].m_F;
        const auto& y1 = points[0].m_G;
        const auto& y2 = points[1].m_G;

        return (x2 - omega) / (x2 - x1) * y1 + (omega - x1) / (x2 - x1) * y2;
    }

    // 如果omega大于最大x值，使用最后两个点进行外推
    if (omega > x_max)
    {
        const auto& x1 = points[points.size () - 2].m_F;
        const auto& x2 = points[points.size () - 1].m_F;
        const auto& y1 = points[points.size () - 2].m_G;
        const auto& y2 = points[points.size () - 1].m_G;

        return (x2 - omega) / (x2 - x1) * y1 + (omega - x1) / (x2 - x1) * y2;
    }

    // 在范围内
    for (size_t i = 0; i < points.size () - 1; ++i)
    {
        const auto& xi = points[i].m_F;
        const auto& xj = points[i + 1].m_F;
        const auto& yi = points[i].m_G;
        const auto& yj = points[i + 1].m_G;

        if (omega >= xi && omega <= xj)
        {
            return (xj - omega) / (xj - xi) * yi + (omega - xi) / (xj - xi) * yj;
        }
    }

    SGError << "error omega range \n";
    return 0.0;
}

bool SG::DataStructure::FEM::operator== (const TABDMP1Data::DAMPData& val1, const TABDMP1Data::DAMPData& val2)
{
    FIELD_COMPARE (m_F)
    FIELD_COMPARE (m_G)
    return true;
}

bool SG::DataStructure::FEM::operator== (const TABDMP1Data& val1, const TABDMP1Data& val2)
{
    FIELD_COMPARE (m_id)
    FIELD_COMPARE (m_domainId)
    return true;
}